package ge.home.app.Camera;


import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;

import ge.home.app.R;
import ge.home.app.Utils;

/**
 * Created by bacho on 3/26/14.
 */
public class CameraFns extends Fragment {
    private Context context;
    private static Boolean flashStatus = false;
    private static Boolean ledStatus = false;
    private static Boolean hasFlashLightFeature = null;
    private Camera camera;
    private Camera.Parameters params;
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getActivity();
        hasFlashLightFeature = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        rootView = inflater.inflate(R.layout.fragment_battary, container, false);
        final Button flashButton = (Button) rootView.findViewById(R.id.flash_button);
        final SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.screen_brightness_control);

        final Button ledButton = (Button) rootView.findViewById(R.id.led_button);
        flashButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg;
                if (hasFlashLightFeature.equals(true)) {
                    msg = "Flashlight " + flashButton.getText();
                    if (flashStatus.equals(false)) {
                        startFlash();
                        flashButton.setText("Off");
                    } else {
                        stopFlash();
                        flashButton.setText("On");
                    }
                } else
                    msg = "Device has not flashlight feature!";
                Utils.showToast(getActivity(), msg, 1000);
            }
        });
        ledButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = "Led " + ledButton.getText();
                if (ledStatus.equals(false)) {
                    ledOn();
                    seekBar.setProgress(100);
                    ledButton.setText("Off");
                } else {
                    ledOff();
                    seekBar.setProgress(0);
                    ledButton.setText("On");
                }
                Utils.showToast(getActivity(), msg, 1000);
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (ledStatus.equals(true))
                    setBrightness(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        return rootView;
    }

    private void startFlash() {
        camera = Camera.open();
        params = camera.getParameters();
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        camera.autoFocus(new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
            }
        });
        flashStatus = true;
    }

    private void stopFlash() {
        camera.stopPreview();
        camera.release();
        camera = null;
        flashStatus = false;
    }

    private void setBrightness(int perc) {
        WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
        layoutParams.screenBrightness = perc * 1f / 100;
        getActivity().getWindow().setAttributes(layoutParams);
    }

    private void ledOn() {
        rootView.setBackgroundColor(Color.WHITE);
        rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        rootView.setKeepScreenOn(true);
        setBrightness(100);
        ledStatus = true;
    }

    private void ledOff() {
        rootView.setBackgroundColor(Color.BLACK);
        rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
        rootView.setKeepScreenOn(false);
        setBrightness(-100);
        ledStatus = false;
    }
}
